#include "global.h"
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_Value_Slider.H>
#include <FL/Fl_Tabs.H>
#include <FL/Fl_Group.H>

#include "SoundWorld.h"
#include "Sound.h"

#include <stdio.h>
#include "math.h"


class MyApp {
    Fl_Window *win;
    Fl_Menu_Bar *menubar;
    SoundWorld *mygl;
    Sound sound;
    Fl_Group* pluginsTab;

    // Static menu callback
    static void Menu_CB(Fl_Widget*w, void*data)
    {
        MyApp *o = (MyApp*)data;
        o->Menu_CB2();
    }

    // Callback method with class access
    void Menu_CB2()
    {
        char picked[80];
        menubar->item_pathname(picked, sizeof(picked)-1);
        printf("CALLBACK: You picked '%s'\n", picked);

        // How to handle callbacks..
        if ( strcmp(picked, "File/Save") == 0 ) sound.save();
        if ( strcmp(picked, "File/Open") == 0 ) sound.load();
        if ( strcmp(picked, "File/Quit") == 0 ) quit();
        if ( strcmp(picked, "Help/Help") == 0 )
        {
            pluginsTab->add(sound.plugins[1]->displayWindow());
            printf("Help goes here\n");
        }
    }


public:
    MyApp()
    {
        // Make the app window and menu bar w/callbacks
        win = new Fl_Window(720, 486);
        menubar = new Fl_Menu_Bar(0, 0, win->w(), 25);
        menubar->add("File/Open", 0, Menu_CB, (void*)this);	// userdata is always 'this'
        menubar->add("File/Save", 0, Menu_CB, (void*)this);	// userdata is always 'this'
        menubar->add("File/Quit", 0, Menu_CB, (void*)this);
        menubar->add("Help/Help", 0, Menu_CB, (void*)this);
        Fl_Tabs* o = new Fl_Tabs(0, 25, win->w(), win->h()-25);
        {
            Fl_Group* o = new Fl_Group(0, 25, win->w(), win->h()-50, "Notes");
            {
                mygl = new SoundWorld(0, 25, win->w(), win->h()-50);
            }
            o->end();
        }

        {
            pluginsTab = new Fl_Group(0, 25, win->w(), win->h()-50, "Plugins");
            pluginsTab->hide();
            {

            }
            pluginsTab->end();
        }
        o->end();


        win->end();
        win->resizable(win);
        win->show();


        sound.vpGlWindow = (void*)(&mygl->glWindow);
        mygl->glWindow.pSound = &sound;
        sound.openAudio();

    }

    void quit()
    {
        sound.closeAudio();
        exit(0);
    }

    ~MyApp()
    {
        sound.closeAudio();
    }
};


/// MAIN ///////////////////////////////////////

int main()
{
    gIni.LoadFile("godplug.ini");

    MyApp app;
    Fl::run();
    app.quit();

}


